<?php
class Application_Model_Filter_VisitingSearch {
    //快速搜索
    public $quick;
    //开始时间
    public $starttime;
    //结束时间
    public $endtime;
    //城市Id
    public $cityId;
    //市级Id
    public $townId;
    //开始重量
    public $startWeight;
    //结束重量
    public $endWeight;
    //开始体积
    public $startVolume;
    //结束体积
    public $endVolume;
    //开始金额
    public $startMoney;
    //结束金额
    public $endMoney;
    //类型
    public $isWay;
    //后台状态
    public $interiorId;
    //用户id 
    public $userId;
    //状态 前台分已完成和已取消的
    public $status;
    //是否已删除
    public $isDel;
    //是否 待补交
    public $topay;
    
    public $problem;

    public $pstatus;
    
    function getSearchFileds() {
        $where = array();
        $and = "";
        if($this->quick){
            $quick = trim($this->quick);
            $num = strlen($quick);
//            $where[' or'] = array('userid'=>user,'ass'=>444);
            $where["( "] = 1;
            $or = " and ";
            if($num == 5){
//                $uwhere[ ' storeno = '] = $quick;
                $user = Application_Model_User::bystore($quick);
                if ($user) {
                    $where[$or . ' userId = '] = $user->id;
                    $or = " or ";
                }else{
                    $where[$or . ' userId = '] = "";
                    $or = " or ";
                }
            }else if($num == 12){
                $where[$or . " inlandLogistics = "] = $quick;
                $or = " or ";
            }  else {
                $where[$or . ' visitingNo = '] = $quick;
                $or = " or ";
                $where[$or . ' sfNo = '] = $quick;
            }        
            $where[") and "] = "1=1";
            $and = ' and ';
        }
        if ($this->starttime != '') {
            $where[$and  . 'datePaid >='] = $this->starttime . " 00:00:00";
            $and = ' and ';
        }
        if ($this->endtime != '') {
            $where[$and  . 'datePaid <='] = $this->endtime . " 23:59:59";
            $and = ' and ';
        }
        if($this->cityId != ""){
            $where[$and . ' inlandCityId = '] = $this->cityId;
            $and = ' and ';
        }
        if($this->townId != ""){
            $where[$and . ' inlandTownId = '] = $this->townId;
            $and = ' and ';
        }
        if($this->startWeight != null && $this->endWeight != null)
        {
            $where[$and." estimateWeight >= "] = $this->startWeight*1000;
            $and = " and ";
            $where[$and." estimateWeight <= "] = $this->endWeight*1000;
        }
        if($this->startVolume != null && $this->endVolume != null)
        {
            $where[$and." estimateVolume >= "] = $this->startVolume*1000;
            $and = " and ";
            $where[$and." estimateVolume <= "] = $this->endVolume*1000;
        }
        if($this->startMoney != null && $this->endMoney != null)
        {
            $where[$and." inlandMoney >= "] = $this->startMoney;
            $and = " and ";
            $where[$and." inlandMoney <= "] = $this->endMoney;
        }
        if($this->isWay !== "" && $this->isWay !== null){
            $where[$and." isWay = "] = $this->isWay;
            $and = " and ";
        }
        if($this->interiorId !== "" && $this->interiorId !== null){
            $where[$and." interior = "] = $this->interiorId;
            $and = " and ";
        }
        if($this->userId != "")
        {
            $where[$and." userId = "] = $this->userId;
            $and = " and ";
        }
        if($this->status != "")
        {
            if($this->status == Application_Model_VisitingServer::STATUS_Cancel){
                $where[$and." status = "] = $this->status;
                $and = " and ";
            } else if ($this->status == Application_Model_VisitingServer::STATUS_InStore){
                $where[$and." status >= "] = $this->status;
                $and = " and ";
            }
        }
        if($this->isDel != "")
        {
            $where[$and." isDel = "] = $this->isDel;
            $and = " and ";
        }else{
            $where[$and." (isDel is null or isDel = 0) and "] = 1;
            //$where[$and." (isDel is null or isDel = 0 or isDel = 1) and "] = 1;
            $and = " and ";
        }
        if($this->problem != "")
        {
            $where[$and." problem = "] = $this->problem;
            $and = " and ";
        } else {
            $where[$and." (problem != 3 or problem is null ) and "] = 1;
            $and = " and ";
        }
        return $where;
    }

    public function getproblemSearchFileds($id){
        $where = array();
        $and = "";
        if($this->problem != "")
        {
            $where[$and." problem = "] = $this->problem;
            $and = " and ";
        }
        if($this->pstatus != ""){
            $where[$and." id in "] = '('.$id.')';
            $and = " and ";
        }
        return $where;
    }
}
